<?php
class LiveBoard {

	//Clear the liveboard
	public function clearLiveBoard()
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		//Xoa het du lieu cac bang hien thi
		$sql = "TRUNCATE TABLE liveboard";
		mysql_query($sql);
		$sql = "TRUNCATE TABLE currentmatch";
		mysql_query($sql);
		$sql = "TRUNCATE TABLE currentprice";
		mysql_query($sql);
		
		//Dien gia tri ban dau vao
		$sql = "SELECT * FROM stock";
		if(!($result = mysql_query($sql))) die(mysql_error());
		while($row = mysql_fetch_array($result))
		{
			$price = floatval($row['Price']);
			$pricemax = $price * 1.1;
			$pricemin = $price * 0.9;
			$sql = "INSERT INTO liveboard (Code, Price, PriceMax, PriceMin) VALUES ('".$row['Code']."','".$row['Price']."','$pricemax','$pricemin')";
			mysql_query($sql);
			$sql = "INSERT INTO currentmatch (StockID, PriceBegin) VALUES (".$row['StockID'].",'".$row['Price']."')";
			mysql_query($sql);
			$sql = "INSERT INTO currentprice (StockID, Average, High, Low) VALUES (".$row['StockID'].",'0','0','0')";
			mysql_query($sql);
		}
	}

	//Show the liveboard
	public function showLiveBoard()
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$sql = "SELECT * FROM liveboard";
		if(!($result = mysql_query($sql))) die(mysql_error());
		echo "<table width='100%' border='1'>";
		echo "<tr>
			    <th rowspan='2' scope='col'>Mã CK</th>
			    <th rowspan='2' scope='col'>T.C</th>
			    <th rowspan='2' scope='col'>Trần</th>
			    <th rowspan='2' scope='col'>Sàn</th>
			    <th colspan='6' scope='col'>Bên mua</th>
			    <th colspan='3' scope='col'>Khớp lệnh</th>
			    <th colspan='6' scope='col'>Bên bán</th>
			    <th rowspan='2' scope='col'>TB</th>
			    <th rowspan='2' scope='col'>Cao</th>
			    <th rowspan='2' scope='col'>Thấp</th>
			  </tr>
			  <tr>
			    <td>Giá 3</td>
			    <td>KL 3</td>
			    <td>Giá 2</td>
			    <td>KL 2</td>
			    <td>Giá 1</td>
			    <td> KL 1</td>
			    <td>+/-</td>
			    <td>Giá</td>
			    <td>KL</td>
			    <td>Giá 1</td>
			    <td>KL 1</td>
			    <td>Giá 2</td>
			    <td>KL 2</td>
			    <td>Giá 3</td>
			    <td>KL 3</td>
			  </tr>";
		while($row = mysql_fetch_array($result))
		{
			echo "<tr>";
			echo "<td> " . $row['Code'] . "</td>";
			echo "<td> " . floatval( $row['Price']) . "</td>";
			echo "<td> " . floatval( $row['PriceMax']) . "</td>";
			echo "<td> " . floatval($row['PriceMin']). "</td>";
			echo "<td> " . floatval($row['BuyPrice3']) . "</td>";
			echo "<td> " . (floatval($row['BuyAmount3']) / 10) . "</td>";
			echo "<td> " . floatval($row['BuyPrice2']) . "</td>";
			echo "<td> " . (floatval($row['BuyAmount2']) / 10) . "</td>";
			if (strcmp($row['BuyPrice1'],"ATC") == 0 || strcmp($row['BuyPrice1'],"ATO") == 0)
			echo "<td> " . $row['BuyPrice1'] . "</td>";
			else
			echo "<td> " . floatval($row['BuyPrice1']) . "</td>";
			echo "<td> " . (floatval($row['BuyAmount1']) / 10) . "</td>";
			echo "<td> " . floatval($row['Diff']) . "</td>";
			echo "<td> " . floatval($row['DiffPrice']) . "</td>";
			echo "<td> " . (floatval($row['DiffAmount']) / 10) . "</td>";
			if (strcmp($row['SellPrice1'],"ATC") == 0 || strcmp($row['SellPrice1'],"ATO") == 0)
			echo "<td> " . $row['SellPrice1'] . "</td>";
			else
			echo "<td> " . floatval($row['SellPrice1']) . "</td>";
			echo "<td> " . (floatval($row['SellAmount1']) / 10) . "</td>";
			echo "<td> " . floatval($row['SellPrice2']) . "</td>";
			echo "<td> " . (floatval($row['SellAmount2']) / 10) . "</td>";
			echo "<td> " . floatval($row['SellPrice3']) . "</td>";
			echo "<td> " . (floatval($row['SellAmount3']) / 10) . "</td>";
			echo "<td> " . floatval($row['Average']) . "</td>";
			echo "<td> " . floatval($row['High']) . "</td>";
			echo "<td> " . floatval($row['Low']) . "</td>";
			echo "</tr>";
		}
		echo "</table>";
	}
	//-----------------------------------------------------------------------------------------------------------------------------------------
	//	Write the value into liveboad with LO
	public function setLiveBoard_LO($code)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$stock = new Stock();
		$id = $stock->findIDStockByCode($code);
		//Buy
		$sql = "SELECT Price, SUM(Amount) FROM buyLO WHERE stockId = ".$id." GROUP BY Price ORDER BY Price DESC";
		if(!($result = mysql_query($sql))) die(mysql_error());
		for ($in = 1; $in <= 3; $in++)
		{
			$price = "0";
			$amount = "0";
			if ($row = mysql_fetch_array($result))
			{
				$price = $row['Price'];
				$amount = $row['SUM(Amount)'];
			}
			$sql = "UPDATE liveboard SET BuyPrice".$in." = '".$price."', BuyAmount".$in." = '".$amount."' WHERE code = '".$code."'";
			mysql_query($sql);

		}
		//Sell
		$sql = "SELECT Price, SUM(Amount) FROM sellLO WHERE stockId = ".$id." GROUP BY Price ORDER BY Price ASC";
		if(!($result = mysql_query($sql))) die(mysql_error());
		for ($in = 1; $in <= 3; $in++)
		{
			$price = "0";
			$amount = "0";
			if ($row = mysql_fetch_array($result))
			{
				$price = $row['Price'];
				$amount = $row['SUM(Amount)'];
			}
			$sql = "UPDATE liveboard SET SellPrice".$in." = '".$price."', SellAmount".$in." = '".$amount."' WHERE code = '".$code."'";
			mysql_query($sql);

		}
		
		$stock = new Stock();
		$stockid = $stock->findIDStockByCode($code);
		//Statistic currentmatch
		$sql = "SELECT  Compare, Price, Amount FROM currentmatch WHERE StockID = ".$stockid;
		if(!($result = mysql_query($sql))) die(mysql_error());
		else
		{
			$row = mysql_fetch_array($result);
			$sql = "UPDATE liveboard SET Diff= '".$row['Compare']."', DiffPrice= '".$row['Price']."', DiffAmount= '".$row['Amount']."' WHERE code = '".$code."'";
			mysql_query($sql);

		}
		
		//Statistic currentPrice
		$sql = "SELECT  Average, High, Low FROM currentprice WHERE StockID = ".$stockid;
		if(!($result = mysql_query($sql))) die(mysql_error());
		else
		{
			$row = mysql_fetch_array($result);
			$sql = "UPDATE liveboard SET Average= '".$row['Average']."', High= '".$row['High']."', Low= '".$row['Low']."' WHERE code = '".$code."'";
			mysql_query($sql);

		}
	}
	//-----------------------------------------------------------------------------------------------------------------------------------------

	function checkSellAT($id, $code)
	{
		$sql = "SELECT Price, SUM(Amount) FROM sellAT WHERE (stockId = ".$id.") AND ((price='ATC') OR (price='AT0')) GROUP BY Price";
		if(!($result = mysql_query($sql))) die(mysql_error());
		$nRow = mysql_num_rows($result);
		if ($nRow > 0)
		{
			$row = mysql_fetch_array($result);
			$price = $row['Price'];
			$amount = $row['SUM(Amount)'];
			$sql = "UPDATE liveboard SET SellPrice1 = '".$price."', SellAmount1= '".$amount."' WHERE code = '".$code."'";
			mysql_query($sql);
			return 2;
		}
		return 1;
	}

	//	Write the value into liveboad with AT
	public function setLiveBoard_AT($code)
	{
		include $_SERVER['DOCUMENT_ROOT'].'scue'.'/configuration.php';
		$stock = new Stock();
		$id = $stock->findIDStockByCode($code);
		//Buy
		$sql = "SELECT Price, SUM(Amount) FROM buyAT WHERE stockId = ".$id." GROUP BY Price ORDER BY Price DESC";
		if(!($result = mysql_query($sql))) die(mysql_error());
		for ($in = 1; $in <= 3; $in++)
		{
			$price = "0";
			$amount = "0";
			if ($row = mysql_fetch_array($result))
			{
				$price = $row['Price'];
				$amount = $row['SUM(Amount)'];
			}
			$sql = "UPDATE liveboard SET BuyPrice".$in." = '".$price."', BuyAmount".$in." = '".$amount."' WHERE code = '".$code."'";
			mysql_query($sql);

		}
		//Sell
		$sql = "SELECT Price, SUM(Amount) FROM sellAT WHERE stockId = ".$id." GROUP BY Price ORDER BY Price ASC";
		if(!($result = mysql_query($sql))) die(mysql_error());
		for ($in = $this->checkSellAT($id, $code); $in <= 3; $in++)
		{
			$price = "0";
			$amount = "0";
			if ($row = mysql_fetch_array($result))
			{
				$price = $row['Price'];
				$amount = $row['SUM(Amount)'];
				if (strcmp($price,"ATC") == 0 || strcmp($price,"ATO") == 0)
				{
					$price = "0";
					$amount = "0";
				}
			}
			$sql = "UPDATE liveboard SET SellPrice".$in." = '".$price."', SellAmount".$in." = '".$amount."' WHERE code = '".$code."'";
			mysql_query($sql);

		}
	}

}
?>